/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "particledata.h"
#include "QDebug"

#include "timermanager.h"
#include "visualeffectdata.h"

/*! \class ParticleData
 *
 * \c ParticleData class is used for managing the particle effect.
 * Particle effects are stored in the \c ParticleData::particleBuffer array.
 */

/*! \c ParticleData constructor. Initializes the particle buffer.
 *
 * \param viewRangeX    Game area view range (x-axis)
 * \param viewRangeY    Game area view range (y-axis)
 * \param parent        Parent object
 */
ParticleData::ParticleData(FPtype viewRangeX, FPtype viewRangeY, QObject *parent) 
    : QObject(parent)
    , particleBuffer(SMASHMINER_CONFIG_PARTICLE_BUFFER_CAPACITY)
    , pBufferA(SMASHMINER_CONFIG_PARTICLE_BUFFER_CAPACITY)
    , pBufferB(SMASHMINER_CONFIG_PARTICLE_BUFFER_CAPACITY)
    , particleWriteIndex(0)
    , gameViewRangeX(viewRangeX)
    , gameViewRangeY(viewRangeY)
{
    for (int i = 0;i < SMASHMINER_CONFIG_PARTICLE_BUFFER_CAPACITY;i++) {
        particleBuffer[i].particleType = ParticleTypeNULL;
        particleBuffer[i].startFrame = -10;
        particleBuffer[i].startTime = -10.0 * SMASHMINER_CONFIG_PARTICLE_LIFETIME;
        particleBuffer[i].startPosX = -10.0 * gameViewRangeX;
        particleBuffer[i].startPosY = -10.0 * gameViewRangeY;
        particleBuffer[i].startVelX = 0.0;
        particleBuffer[i].startVelY = 0.0;
    }

    particleWriteIndex = 0;
}

/*! Adds particle effect to particle buffer.
 *
 * \param startFrame    Particle effect start frame (usually current game frame as in
 *                      \c GameEngine::totalFrameCounter).
 * \param startTime     Particle effect start time (usually current game time as in
 *                      \c GameEngine::totalTimeCounter).
 * \param positionX     X-coordinate for particle effect position.
 * \param positionY     Y-coordinate for particle effect position.
 * \param positionZ     Z-coordinate for particle effect position.
 * \param velocityX     Particle effect velocity in X-axis.
 * \param velocityY     Particle effect velocity in Y-axis.
 * \param friction      Particle effect friction.
 * \param size          Particle size.
 */
void ParticleData::addParticle(int startFrame, GLfloat startTime,
                               GLfloat positionX, GLfloat positionY, GLfloat positionZ,
                               GLfloat velocityX, GLfloat velocityY, GLfloat friction, GLfloat size)
{
#ifdef SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    if (particleBuffer[particleWriteIndex].startTime >= 0.0) {
        const GLfloat timeLimit = SMASHMINER_CONFIG_REWIND_PERIOD_SECONDS + SMASHMINER_CONFIG_PARTICLE_LIFETIME;
        if (startTime - particleBuffer[particleWriteIndex].startTime < timeLimit) {
            static int suppressWarning = 0;
            if (suppressWarning++ % 1000 == 0) 
                qDebug() << "GameEngine::addParticle : WARNING : particle buffer is too small.";
        }
    }

    if (size < 0.0) {
        qDebug() << "GameEngine::addParticle() : size is negative.";
        exit(EXIT_FAILURE);
    }

#endif  // SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    const GLfloat frict = friction / 1000.0;

    particleBuffer[particleWriteIndex].particleType = ParticleTypeNormal;

    particleBuffer[particleWriteIndex].startFrame = startFrame;
    particleBuffer[particleWriteIndex].startTime = startTime;

    particleBuffer[particleWriteIndex].startPosX = positionX;
    particleBuffer[particleWriteIndex].startPosY = positionY;

    particleBuffer[particleWriteIndex].startVelX = velocityX;
    particleBuffer[particleWriteIndex].startVelY = velocityY;

    particleBuffer[particleWriteIndex].startFrict = frict;

    // setup the initial position and velocity.

    pBufferA[particleWriteIndex].positionX = positionX;
    pBufferA[particleWriteIndex].positionY = positionY;
    pBufferA[particleWriteIndex].positionZ = positionZ;    // not stored elsewhere.

    particleBuffer[particleWriteIndex].velocityX = velocityX;
    particleBuffer[particleWriteIndex].velocityY = velocityY;

    pBufferB[particleWriteIndex].size = size;       // not stored elsewhere.
    pBufferB[particleWriteIndex].friction = frict;

    // no need to write alpha values into pBufferB here,
    // since it is done just prior to painting the particles.

    if (++particleWriteIndex >= SMASHMINER_CONFIG_PARTICLE_BUFFER_CAPACITY)
        particleWriteIndex = 0;
}

/*! Adds a particle effect for black hole.
 *
 * \param startFrame    Particle effect start frame (usually current game frame as in
 *                      \c GameEngine::totalFrameCounter).
 * \param startTime     Particle effect start time (usually current game time as in
 *                      \c GameEngine::totalTimeCounter).
 * \param distance      Particle distance from black hole center.
 * \param angle         Angle in radians.
 * \param radvel        Particle effect velocity.
 * \param size          Particle size.
 */
void ParticleData::addParticleBlackHole(int startFrame, GLfloat startTime, GLfloat distance, 
                                        GLfloat angle, GLfloat radvel, GLfloat size)
{
#ifdef SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    if (particleBuffer[particleWriteIndex].startTime >= 0.0) {
        const GLfloat timeLimit = SMASHMINER_CONFIG_REWIND_PERIOD_SECONDS 
            + SMASHMINER_CONFIG_PARTICLE_LIFETIME;
        if (startTime - particleBuffer[particleWriteIndex].startTime < timeLimit)
            qDebug() << "GameEngine::addParticleBH : WARNING : particle buffer is too small.";
    }

    if (size < 0.0) {
        qDebug() << "GameEngine::addParticle() : size is negative.";
        exit(EXIT_FAILURE);
    }

#endif  // SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    particleBuffer[particleWriteIndex].particleType = ParticleTypeBlackHole;

    particleBuffer[particleWriteIndex].startFrame = startFrame;
    particleBuffer[particleWriteIndex].startTime = startTime;

    particleBuffer[particleWriteIndex].startPosX = distance;
    particleBuffer[particleWriteIndex].startPosY = radvel;

    particleBuffer[particleWriteIndex].startFrict = angle;

    // set the particle size negative so that the GLSL program
    // knows to skip the BlackHole effect for this particle.

    pBufferB[particleWriteIndex].size = -size;      // not stored elsewhere ; IS NEGATIVE!
    pBufferB[particleWriteIndex].friction = angle;

    // no need to write alpha values into pBufferB here,
    // since it is done just prior to painting the particles.

    if (++particleWriteIndex >= SMASHMINER_CONFIG_PARTICLE_BUFFER_CAPACITY)
        particleWriteIndex = 0;
}

/*! Adds a particle effect for hyper jump.
 *
 * \param startFrame    Particle effect start frame (usually current game frame as in
 *                      \c GameEngine::totalFrameCounter).
 * \param startTime     Particle effect start time (usually current game time as in
 *                      \c GameEngine::totalTimeCounter).
 * \param angle         Angle in radians.
 * \param size          Particle size.
 * \param hyperJumpMode Hyper jump mode (Hyper jump in/out)
 */
void ParticleData::addParticleHyperJump(int startFrame, GLfloat startTime, GLfloat angle, GLfloat size, int hyperJumpMode)
{
#ifdef SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    if (particleBuffer[particleWriteIndex].startTime >= 0.0) {
        const GLfloat timeLimit = SMASHMINER_CONFIG_REWIND_PERIOD_SECONDS + SMASHMINER_CONFIG_PARTICLE_LIFETIME;
        if (startTime - particleBuffer[particleWriteIndex].startTime < timeLimit)
            qDebug() << "GameEngine::addParticleBH : WARNING : particle buffer is too small.";
    }

    if (size < 0.0) {
        qDebug() << "GameEngine::addParticle() : size is negative.";
        exit(EXIT_FAILURE);
    }

    if (hyperJumpMode != 0 && hyperJumpMode != 1) {
        qDebug() << "GameEngine::addParticleHJ() : unknown hyperJumpMode " << hyperJumpMode;
        exit(EXIT_FAILURE);
    }

    if (hyperJumpMode == 0 && TimerManager::hyperJumpOutData == NULL) {
        qDebug() << "GameEngine::addParticleHJ() : hyperJumpMode == 0 && hyperJumpOutData == NULL";
        exit(EXIT_FAILURE);
    }

    if (hyperJumpMode == 1 && TimerManager::hyperJumpInData == NULL) {
        qDebug() << "GameEngine::addParticleHJ() : hyperJumpMode == 1 && hyperJumpInData == NULL";
        exit(EXIT_FAILURE);
    }

#endif  // SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    particleBuffer[particleWriteIndex].particleType = ParticleTypeHyperJump;

    particleBuffer[particleWriteIndex].startFrame = startFrame;
    particleBuffer[particleWriteIndex].startTime = startTime;

    if (hyperJumpMode == 0) {
        particleBuffer[particleWriteIndex].startPosX = TimerManager::hyperJumpOutData->posX();
        particleBuffer[particleWriteIndex].startPosY = TimerManager::hyperJumpOutData->posY();
    } else if (hyperJumpMode == 1) {
        particleBuffer[particleWriteIndex].startPosX = TimerManager::hyperJumpInData->posX();
        particleBuffer[particleWriteIndex].startPosY = TimerManager::hyperJumpInData->posY();
    }

    particleBuffer[particleWriteIndex].startFrict = angle;

    // set the particle size negative so that the GLSL program
    // knows to skip the BlackHole effect for this particle.

    pBufferB[particleWriteIndex].size = -size;      // not stored elsewhere ; IS NEGATIVE!

    pBufferB[particleWriteIndex].friction = (GLfloat) hyperJumpMode;

    // no need to write alpha values into pBufferB here,
    // since it is done just prior to painting the particles.

    if (++particleWriteIndex >= SMASHMINER_CONFIG_PARTICLE_BUFFER_CAPACITY)
        particleWriteIndex = 0;
}
